//! 显示API密钥信息
//! 
//! 用于调试和验证API密钥配置

use binance_sdk_unified::{
    client::BinanceClientBuilder, ProductType, Environment, Result,
};

#[tokio::main]
async fn main() -> Result<()> {
    // 初始化日志
    tracing_subscriber::fmt::init();

    println!("🔑 API密钥信息显示");
    println!("================================");

    // 从环境变量获取API密钥
    let api_key = std::env::var("BINANCE_API_KEY").unwrap_or_else(|_| "test_api_key".to_string());
    let secret_key = std::env::var("BINANCE_SECRET_KEY").unwrap_or_else(|_| "test_secret_key".to_string());
    let product_type = std::env::var("BINANCE_PRODUCT_TYPE").unwrap_or_else(|_| "Spot".to_string());
    let environment = std::env::var("BINANCE_ENVIRONMENT").unwrap_or_else(|_| "Demo".to_string());

    println!("📋 当前配置:");
    println!("   API Key: {}", if api_key.len() > 8 { 
        format!("{}...{}", &api_key[..4], &api_key[api_key.len()-4..]) 
    } else { 
        api_key.clone() 
    });
    println!("   Secret Key: {}", if secret_key.len() > 8 { 
        format!("{}...{}", &secret_key[..4], &secret_key[secret_key.len()-4..]) 
    } else { 
        secret_key.clone() 
    });
    println!("   产品类型: {}", product_type);
    println!("   环境: {}", environment);

    // 显示完整的API密钥（仅用于调试）
    println!("\n🔍 完整API密钥信息 (仅用于调试):");
    println!("   API Key: '{}'", api_key);
    println!("   Secret Key: '{}'", secret_key);
    println!("   API Key 长度: {}", api_key.len());
    println!("   Secret Key 长度: {}", secret_key.len());

    // 检查是否是测试密钥
    if api_key == "test_api_key" || secret_key == "test_secret_key" {
        println!("\n⚠️  警告: 正在使用测试API密钥");
        println!("   要使用真实API密钥，请设置环境变量:");
        println!("   export BINANCE_API_KEY='your_real_api_key'");
        println!("   export BINANCE_SECRET_KEY='your_real_secret_key'");
    } else {
        println!("\n✅ 使用自定义API密钥");
    }

    // 尝试创建客户端
    let product_type_enum = match product_type.as_str() {
        "Spot" => ProductType::Spot,
        "UsdM" => ProductType::UsdM,
        "CoinM" => ProductType::CoinM,
        "Options" => ProductType::Options,
        "Portfolio" => ProductType::Portfolio,
        _ => ProductType::Spot,
    };

    let environment_enum = match environment.as_str() {
        "Live" => Environment::Live,
        "Demo" => Environment::Demo,
        _ => Environment::Demo,
    };

    println!("\n🔧 创建客户端...");
    let client = BinanceClientBuilder::new(
        api_key,
        secret_key,
        product_type_enum,
    )
    .environment(environment_enum)
    .timeout(30)
    .retry_count(3)
    .build()?;

    println!("✅ 客户端创建成功！");
    println!("   REST URL: {}", client.config().rest_base_url());
    println!("   WebSocket URL: {}", client.config().ws_base_url());
    println!("   API前缀: {}", client.config().product_type.base_api_prefix());

    Ok(())
}
